/*
 private final static SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 */
package org.example.utils;


import org.example.common.BusinessException;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @author fuchangshun
 * @date 2018-12-05 17:49:10
 */
public final class FuDateUtil {

    private final static String SIMPLE_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    private final static String SIMPLE_DAY_FORMAT = "yyyy-MM-dd";
    private final static String YYYY_MM_DD = "yyyyMMdd";
    private final static String YYYY_MM = "yyyyMM";
    private final static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
    private final static String MMDDHHMMSS = "MMddHHmmss";
    private final static String SIMPLE_MONTH_FORMAT = "yyyy-MM";

    /**
     * 获取当天的开始时间
     *
     * @return 当天0点0分0秒
     */
    public static Date getStartTime() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }
    public static Date dayBefo (int count){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        //过去一月
        c.setTime(new Date());
        c.add(Calendar.DATE, -count);
        Date m = c.getTime();

        return m;
    }
    /**
     * 根据不同的格式解析日期
     *
     * @param day 日期
     * @param format 日期格式
     * @return
     */
    public static Date parseDay(String day, String format) {
        try {
            return new SimpleDateFormat(format).parse(day);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new BusinessException("时间解析失败");
        }
    }

    /**
     * 获取 yyyy-MM-dd HH:mm:ss 格式的字符串
     *
     * @return 时间字符串
     */
    public static String getDateString() {
        return new SimpleDateFormat(SIMPLE_DATE_FORMAT).format(new Date());
    }

    /**
     * 获取 yyyy-MM-dd 格式的字符串
     *
     * @return 时间字符串
     */
    public static String getDateString2() {
        return new SimpleDateFormat(SIMPLE_MONTH_FORMAT).format(new Date());
    }
    /**
     * 日解析
     *
     * @param day 日字符串
     * @return date
     */
    public static Date parseSimpleDay(String day) {
        try {
            return new SimpleDateFormat(SIMPLE_DAY_FORMAT).parse(day);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new BusinessException("时间解析失败");
        }
    }

    /**
     * 两个时间戳相差几分钟
     * @param newTime 当前时间
     * @param oldTime 过去时间
     * @return
     */
    public static long diffMin(long newTime,long oldTime){
        return (newTime - oldTime)/1000/60;
    }


    public static String getCurrentYear() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        Date date = new Date();
        return sdf.format(date);
    }

    public static String getCurrentMonth() {
        SimpleDateFormat sdf = new SimpleDateFormat("MM");
        Date date = new Date();
        return sdf.format(date);
    }

    public static String getCurrentDay() {
        SimpleDateFormat sdf = new SimpleDateFormat("dd");
        Date date = new Date();
        return sdf.format(date);
    }

    /**
     * 获取 yyyyMMdd 格式的字符串
     *
     * @return 时间字符串
     */
    public static String yyyyMMdd(Date date) {
        return new SimpleDateFormat(YYYY_MM_DD).format(date);
    }
    /**
     * 获取 yyyyMMdd 格式的字符串
     *
     * @return 时间字符串
     */
    public static String yyyyMM(String date) {
        Date parse = null;
        try {
            parse =   new SimpleDateFormat(SIMPLE_DAY_FORMAT).parse(date);
        } catch (ParseException e) {
            throw new BusinessException("时间解析失败");
        }
        return new SimpleDateFormat(YYYY_MM).format(parse);
    }
    public static String YYYYMMDDHHMMSS(Date date) {
        return new SimpleDateFormat(YYYYMMDDHHMMSS).format(date);
    }

    public static String MMDDHHMMSS(Date date) {
        return new SimpleDateFormat(MMDDHHMMSS).format(date);
    }


    public static String getMonthBegin(String a) throws ParseException {

        SimpleDateFormat aDateFormat = new SimpleDateFormat("yyyy-MM");
        Date date = aDateFormat.parse(a);
        Calendar c = Calendar.getInstance();
        c.setTime(date);

        //设置为1号,当前日期既为本月第一天
        c.set(Calendar.DAY_OF_MONTH, 1);
        //将小时至0
        c.set(Calendar.HOUR_OF_DAY, 0);
        //将分钟至0
        c.set(Calendar.MINUTE, 0);
        //将秒至0
        c.set(Calendar.SECOND, 0);
        //将毫秒至0
        c.set(Calendar.MILLISECOND, 0);

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 获取本月第一天的时间
        return format.format(c.getTime());

    }

    public static String getMonthEnd(String a) throws ParseException {
        //设置时间格式
        SimpleDateFormat formatBefore = new SimpleDateFormat("yyyy-MM");
        Date date = formatBefore.parse(a);

        //获得实体类
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        //设置最后一天
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        //将小时至0
        ca.set(Calendar.HOUR_OF_DAY, 23);
        //将分钟至0
        ca.set(Calendar.MINUTE, 59);
        //将秒至0
        ca.set(Calendar.SECOND, 59);

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 获取本月第一天的时间
        return  format.format(ca.getTime());

    }

    public static long getTodayZero() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis();
    }

    public static void main(String[] args) {
        System.out.println(getTodayZero());
    }
}





